import 'package:flutter/material.dart';
import 'package:flutterproject/page/mine/children/order/children/logistics_tracking/data/logistics_track_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class LogisticsTrackingPage extends StatefulWidget {
  const LogisticsTrackingPage({Key? key}) : super(key: key);

  @override
  State<LogisticsTrackingPage> createState() => _LogisticsTrackingPageState();
}

class _LogisticsTrackingPageState extends State<LogisticsTrackingPage> {
  HttpClient client = Get.find<HttpClient>();
  LogisticsTrackBean? bean = LogisticsTrackBean();

  @override
  void initState() {
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xFFF6F6F6),
      width: Get.width,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AppBarCommon(
            title: "物流追踪",
            bgColor: Color(0xFFEDC88B),
            leftColor: Colors.white,
            titleColor: Colors.white,
          ),
          Expanded(
            child: Container(
              margin: MarginPaddingUtil.symmetric(horizontal: 20, vertical: 28),
              padding:
                  MarginPaddingUtil.symmetric(horizontal: 30, vertical: 40),
              color: Colors.white,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      ImageCommon(
                        "/icon/ic_wuliu1",
                        width: 50.width,
                        height: 24.width,
                      ),
                      22.widthBox,
                      TextCommon(
                        bean?.expTextName ?? "",
                        size: 28.sp,
                        lineHeight: 40.sp,
                        fontWeight: FontWeight.bold,
                      )
                    ],
                  ),
                  25.heightBox,
                  TextCommon("单号  ${bean?.mailNo ?? ''}"),
                  80.heightBox,
                  Expanded(
                    child: ListView.separated(
                      shrinkWrap: true,
                      itemCount: bean?.data?.length ?? 0,
                      padding: EdgeInsets.zero,
                      itemBuilder: (context, index) {
                        return Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            ImageCommon(
                              "/icon/${index == 0 ? 'ic_zuixin' : 'ic_zuixin_n'}",
                              size: 35.width,
                            ),
                            30.widthBox,
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  TextCommon(
                                    bean?.data![index].context ?? '',
                                    maxLines: 10,
                                    size: 24.sp,
                                    lineHeight: 35.sp,
                                    color: Color(0xFF414141),
                                  ),
                                  10.heightBox,
                                  TextCommon(
                                    bean?.data![index].time ?? '',
                                    maxLines: 10,
                                    size: 22.sp,
                                    lineHeight: 32.sp,
                                    color: Color(0xFF414141),
                                  ),
                                ],
                              ),
                            )
                          ],
                        );
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return 65.heightBox;
                      },
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Future _getData() async {
    try {
      DialogUtil.showLoading();
      bean = await client.get<LogisticsTrackBean>(AddressManager.express,
          queryParameters: {"id": Get.arguments});
      setState(() {});
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }
}
